package com.pai4j.pgc.dao.community;
import com.pai4j.pgc.entity.community.ResourceEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface IResourceDAO extends JpaSpecificationExecutor<ResourceEntity>, JpaRepository<ResourceEntity, Long> {

    ResourceEntity findAllByResourceId(String resourceId);

    List<ResourceEntity> findAllByCcIdAndReviewStatusInOrderByIdDesc(Long ccId, List<Integer> reviewStatusList);

    /***
     * 查询全部分类下最新资源
     * @param limit
     * @return
     */
    @Query(value = "select res.resource_id from pai4j_resource res " +
            "where res.status = 1 and res.review_status in (1, 2) " +
            "order by review_date desc, review_status desc " +
            "limit :limit ", nativeQuery = true)
    List<String> findRealTimeResourceIdList(Integer limit);

    /***
     * 查询分类下最新资源
     * @param path
     * @param limit 条数
     * @return
     */
    @Query(value = "select res.resource_id from pai4j_resource res " +
            "where res.path = :path and res.status = 1 and res.review_status in (1, 2) " +
            "order by review_date desc, review_status desc " +
            "limit :limit ", nativeQuery = true)
    List<String> findRealTimeResourceIdListByType(String path, Integer limit);

    @Query(value = "select res.* from pai4j_resource res " +
            "where res.cc_id = :ccid and res.status = 1 and res.review_status in (1, 2) " +
            "order by review_date desc, review_status desc " +
            "limit :limit ", nativeQuery = true)
    List<ResourceEntity> findCTCRealTimeResourceIdListByType(Long ccid, Integer limit);

    /***
     * 查询分类下同步首页的资源
     * @param path
     * @param limit 条数
     * @return
     */
    @Query(value = "select res.resource_id from pai4j_resource res " +
            "where res.IS_SYNC_HOME = 1 and res.path = :path and res.status = 1 and res.review_status in (1, 2) " +
            "order by review_date desc, review_status desc " +
            "limit :limit ", nativeQuery = true)
    List<String> findHomeResourceIdListByType(String path, Integer limit);

    @Query(value = "select res.resource_id from pai4j_resource res " +
            "where res.IS_SYNC_HOME = 1 and res.CC_ID = :ccId and res.status = 1 and res.review_status in (1, 2) " +
            "order by review_date desc, review_status desc " +
            "limit :limit ", nativeQuery = true)
    List<String> findCTCHomeResourceIdListByType(Long ccId, Integer limit);

    /***
     * 精品资源榜单
     * @param limit
     * @return
     */
    @Query(value = "select res.resource_id from pai4j_resource res " +
            "where res.IS_BOUTIQUE = 1 and res.status = 1 and res.review_status in (1, 2) " +
            "order by review_date desc, review_status desc " +
            "limit :limit ", nativeQuery = true)
    List<String> findBoutiqueResourceIdList(Integer limit);

    /***
     * 查询一个时间范围内满足搜索条件的资源
     * @param start
     * @param end
     * @return
     */
    @Query(value = "select res from ResourceEntity res where res.status = 1 and res.reviewStatus in (1, 2) and " +
            "(res.reviewDate between ?1 and ?2)")
    List<ResourceEntity> findAllByUpdateDateBetween(Date start, Date end);

    Page<ResourceEntity> findAllByAuthorAndStatus(String author, Integer status, Pageable pageable);

    List<ResourceEntity> findAllByResourceIdIn(List<String> resourceIdList);

    @Query(value = "select count(1) from pai4j_resource res where res.status = 1 and  res.review_status in (1, 2)",
            nativeQuery = true)
    Long countOnlineResource();

    @Query(value = "select count(1) from pai4j_resource res where res.CC_ID = :ccId and res.status = 1 and res.review_status in (1, 2)",
            nativeQuery = true)
    Long countCtcOnlineResource(Long ccId);
}
